package com.brower.project.base

import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.brower.project.R
import com.brower.project.databinding.ActivityBaseBinding
import com.brower.project.databinding.BaseLayoutTitleBinding
import com.brower.project.popup.LoadingView
import java.lang.reflect.ParameterizedType

abstract class BaseActivity<VB : ViewDataBinding, VM : BaseViewModel> : AppCompatActivity() {
    private val mBaseBinding: ActivityBaseBinding by lazy {
        DataBindingUtil.setContentView(
            this,
            R.layout.activity_base
        )
    }

    lateinit var mViewModel: VM
    var mBinding: VB? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mViewModel = ViewModelProvider(this).get(viewModelClass())
        initContent()
        initTitle()
        initView()
        registerUIChange()
        observerListener()
    }

    open fun getVariable(): HashMap<Int, Any?>? = null

    abstract fun layoutId(): Int

    private fun viewModelClass(): Class<VM> {
        val type = javaClass.genericSuperclass
        if (type is ParameterizedType) {
            val tp = type.actualTypeArguments[1]
            return tp as Class<VM>
        }
        throw Throwable("请添加ViewModel")
    }

    open fun initView() {}


    open fun observerListener() {}

    open fun <T> addObserver(data: LiveData<T>, observer: Observer<T>) {
        data.observe(this, observer)
    }

    open fun title(): CharSequence? {
        return null
    }

    private fun registerUIChange() {
        mViewModel.uiChange.finish.observe(this) {
            if (it.isFinish) {
                finish()
            }
        }
        mViewModel.uiChange.toast.observe(this) { showToast(it) }
        mViewModel.uiChange.showLoadView.observe(this) { showLoadView() }
        mViewModel.uiChange.dismissLoadView.observe(this) { dismissLoadView() }
    }


    fun showToast(text: String) {
        Toast.makeText(applicationContext, text, Toast.LENGTH_LONG).show()
    }


    /**
     * 初始化内容
     */
    private fun initContent() {
        if (!mBaseBinding.contentLayout.isInflated) {
            mBaseBinding.contentLayout.viewStub?.layoutResource = layoutId()
            mBaseBinding.contentLayout.viewStub?.inflate()?.apply {
                mBinding = DataBindingUtil.bind(this)
                val variable = getVariable()
                if (variable?.size ?: 0 > 0) {
                    for (i in variable!!) {
                        mBinding?.setVariable(i.key, i.value)
                    }
                }
            }
        }
    }

    /**
     * 设置标题栏
     */
    private fun initTitle() {
        mViewModel.uiChange.titleData.value = title()?.toString()
        if (!mBaseBinding.titleLayout.isInflated) {
            mBaseBinding.titleLayout.viewStub?.inflate()?.let {
                DataBindingUtil.bind<BaseLayoutTitleBinding>(it)?.apply {
                    viewModel = mViewModel
                }
            }
        }
    }

    private fun showLoadView() {
        LoadingView.getInstance(this).showPopupWindow()
    }

    private fun dismissLoadView() {
        LoadingView.getInstance(this).delayDismiss()
    }
}